<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'translation:add'" @click="handleCreate">
          {{ t('新增') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                auth: 'translation:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                auth: 'translation:delete',
                color: 'error',
                popConfirm: {
                  title: t('是否确认删除'),
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <LgDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { onMounted, ref } from 'vue';

  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';

  import LgDrawer from './components/LgDrawer.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  // import { useI18n } from 'vue-i18n';
  import { PageWrapper } from '/@/components/Page';
  import { deleteLanguage, getLanguagePageList, getLanguageTypeList } from '/@/api/system/language';
  import { useModal } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: t('关键字'),
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入名称或编码'),
      },
    },
  ];

  const { notification } = useMessage();
  const [registerDrawer, { openModal }] = useModal();
  const dataRef = ref([]);
  const [registerTable, { reload, setColumns, setProps }] = useTable({
    title: t('翻译管理列表'),
    columns: [],
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
    },
    handleSearchInfoFn(info) {
      const filterData = dataRef.value.filter((item) => {
        for (const key in item) {
          if ((item[key] as string).indexOf(info.keyword) > -1) {
            return item;
          }
        }
      });
      setProps({ dataSource: filterData });
      return info;
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    pagination: { pageSize: 20 },
    actionColumn: {
      width: 80,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });
  onMounted(async () => {
    const treeData = await getLanguageTypeList();
    dataRef.value = await getLanguagePageList();
    const columns: BasicColumn[] = [];
    treeData.forEach((o) => {
      columns.push({
        title: o.name,
        dataIndex: o.code,
        align: 'left',
      });
    });
    setColumns(columns);
    setProps({ dataSource: dataRef.value });
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    await deleteLanguage([record.mapId]);

    dataRef.value = await getLanguagePageList();
    setProps({ dataSource: dataRef.value });
    reload();
    notification.success({
      message: t('提示'),
      description: t('删除成功'),
    }); //提示消息
  }

  async function handleSuccess() {
    dataRef.value = await getLanguagePageList();
    setProps({ dataSource: dataRef.value });
    reload();
  }
</script>
